home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / util / surf / filemenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  12.9 KB  |  627 lines

  1. #include "all.h"
  2. #include "color.h"
  3. #include "edt.h"
  4.  
  5. #ifdef __TURBOC__
  6. #define DASHCHAR 249
  7. #include "bios.h"
  8. #include "conio.h"
  9. #include "dir.h"
  10. #else
  11. #define DASHCHAR '.'
  12. #include "vaxconio.h"
  13. #endif
  14.  
  15. #define true (!false)
  16. #define false 0
  17. #define BEGINDEF extern
  18. /* #include "begin.h" */
  19. #define BCOLOR BLUE
  20. #define FCOLOR MAGENTA
  21. #define HBCOLOR h_bcolor
  22. #define HVCOLOR h_fcolor
  23. #define VCOLOR WHITE
  24.  
  25. int h_bcolor;
  26. int h_fcolor;
  27. int getsize(void);
  28. char *tabtospace(char *s);
  29. int printdash(int i);
  30. char *gledir(char *s);
  31. char *gletopdir(char *s);
  32. int scr_menuval(void);
  33. int scr_menubg(void);
  34. int scr_menuhi(void);
  35. int scr_grey(void);
  36. int gle_redraw(void);
  37. int text_showerror(void);
  38. int text_save(void);
  39. char *strfile(char *s);
  40.  
  41. int vdelete(int i);
  42. int vinsert(int y, char *s);
  43. int m_ifsend(char *s);
  44. int m_sendline(void);
  45. int minit_extras(void);
  46. int minit_extras(void);
  47. int initmenu(void);
  48. int mystrlen(char *s);
  49. int pick_file(char *d, char *w);
  50. int ncpy(char *d, char *s, int n);
  51. int setvstr(char **d, char *s);
  52. int text_inkey(void);
  53.  
  54. struct menu_struct { int x; int y; char *title; int width; int typ; int typ2;
  55.     char *val; char *help; };
  56. typedef struct menu_struct menutype;
  57. struct pmenu_struct {int ci; menutype *menu; };
  58. typedef struct pmenu_struct pmenutype;
  59. pmenutype pmenu[17];
  60. int setmenu(menutype *m,int x, int y, int typ, int typ2, int width, char *title
  61.     , char *val, char *help);
  62. int do_menu(pmenutype *p);
  63. int token_space(void);
  64. int extractmenu(void);
  65. int add_unrecognized(char *s);
  66. int fillinmenu(int nbegin);
  67. int nunrec;
  68.  
  69. #define  PTXT 0
  70. enum {MEND,MTEXT,MFILE,MTOGON,MTOGOFF,MSUB,MRETURN,MNULL};
  71. enum {SNORM,SROL,SNEXTS,SNEXT,SSTR,STOG,SS,SSIZE,SNOBOX,SAMOVE,SSOFF,SSNULL};
  72. int refresh_menu(struct menu_struct *m);
  73. int menu_hilight(struct menu_struct *m);
  74. int menu_norm(struct menu_struct *m);
  75.  
  76. /* menu's
  77.     topmenu
  78.         title,xtitle,ytitle min max dist on off, ticks dticks
  79.         (2) let d = exp
  80.         Data files
  81.         Dn marker ,color, lwidth, lstyle, err,
  82.     xnamesmenu
  83.     ynamesmenu
  84.     fillmenu
  85.     barmenu
  86.     axismenu's
  87. */
  88. read_default(char *result, char *ques, char *dflt)
  89. {
  90.     char s[200];
  91.     int r;
  92.     strcpy(s,ques); strcat(s," ["); strcat(s,dflt); strcat(s,"] ");
  93. /*    r = read_input(result,s);*/
  94.     if (strlen(result)==0) strcpy(result,dflt);
  95.     return r;
  96. }
  97. extern int changed;
  98. addline(char *s)
  99. {
  100.     changed = true;
  101. }
  102. char *mark_names[] = {
  103.     "dot", "square", "fcircle", "club", "diamond", "triangle", "snake"
  104.     ,"otimes", "odot", ""};
  105.  
  106. int init_menucolor(void);
  107. int m_gstart,m_gend;
  108. /*
  109.  
  110.     Getkey/command
  111.         arrowkeys normal
  112.         left and right arrow if editing
  113.         delete
  114.         insert normal character
  115.         enter, change to new menu
  116.         escape exit.
  117.         F1, help key
  118. */
  119. char mbuff[255];
  120.  
  121. extern int iserr;
  122. int hcx,cx,hcy;    /* HIlighted current x,y */
  123. do_menu(pmenutype *ppmenu)
  124. {
  125.     menutype *cm,*cmi,*si=NULL,*mi;
  126.     int citem,emode=false;
  127.     int c;
  128.     cm = ppmenu->menu;
  129.     citem = ppmenu->ci;
  130.     cmi = cm+citem;
  131.     refresh_menu(cm);
  132.     menu_hilight(cmi);
  133.     for (;;) {
  134.      c = text_inkey();
  135.      if (iserr) {fner_clear(); window(1,1,80,24);}
  136.      switch (c) {
  137.        case eescape: /* ESCAPE */
  138.        case equit: /* control c */
  139.         return true;
  140.        case eleft: /* left */
  141.         if (emode) {
  142.             if (cx>0) cx--;
  143.             gotoxy(hcx+cx,hcy);
  144.         } else {
  145.             for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  146.                 if (mi->y==cmi->y && mi->x < cmi->x)
  147.                 if (si!=NULL) {
  148.                     if (mi->x > si->x)
  149.                         si = mi;
  150.                 } else si = mi;
  151.             }
  152.         }
  153.         break;
  154.        case eright: /* right */
  155.         if (emode) {
  156.             if (cx < strlen(cmi->val)) cx++;
  157.             gotoxy(hcx+cx,hcy);
  158.         } else {
  159.             for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  160.                 if (mi->y==cmi->y && mi->x > cmi->x)
  161.                 if (si!=NULL) {
  162.                     if (mi->x < si->x)
  163.                         si = mi;
  164.                 } else si = mi;
  165.  
  166.             }
  167.         }
  168.         break;
  169.        case eup: /* arrow up */
  170.         emode = false;
  171.         for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  172.             if (mi->typ<MNULL) {
  173.              if (si!=NULL) if (mi->y==si->y)
  174.                if (abs(mi->x - cmi->x) <  abs(si->x - cmi->x))
  175.                 si = mi;
  176.              if (mi->y < cmi->y) if (si!=NULL) {
  177.                if (mi->y > si->y) si = mi;
  178.              } else      si = mi;
  179.             }
  180.         }
  181.         break;
  182.        case edown: /* arrow down */
  183.         emode = false;
  184.         for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  185.             if (mi->typ<MNULL) {
  186.              if (si!=NULL) if (mi->y==si->y)
  187.                if (abs(mi->x - cmi->x) <  abs(si->x - cmi->x))
  188.                 si = mi;
  189.              if (mi->y > cmi->y) if (si!=NULL) {
  190.                if (mi->y < si->y) si = mi;
  191.              } else  si = mi;
  192.             }
  193.         }
  194.         break;
  195.       case ehelp: /* f1 help */
  196.         do_help(cmi->help);
  197.         refresh_menu(cm);
  198.         menu_hilight(cmi);
  199.         break;
  200.       case ereturn: /* carriage return */
  201.         if (emode==true && cmi->typ==MFILE) {
  202.             emode = false;
  203.             break;
  204.         }
  205.         emode = false;
  206. doreturn:;
  207.         switch (cmi->typ) {
  208.           case MTEXT:
  209. /*            if (emode) emode = false; else emode = true; */
  210.             break;
  211.           case MTOGON:
  212.             cmi->typ = MTOGOFF;
  213.             menu_hilight(cmi);
  214.             break;
  215.           case MTOGOFF:
  216.             cmi->typ = MTOGON;
  217.             menu_hilight(cmi);
  218.             break;
  219.           case MRETURN:
  220.             goto exit_menu;
  221.           case MFILE:
  222.             if (mystrlen(cmi->val)==0) cmi->val = myallocz(20);
  223.             pick_file(cmi->val,"*.dat");
  224.             refresh_menu(cm);
  225.             menu_hilight(cmi);
  226.             break;
  227.           case MSUB: /* SUB MENU */
  228.             if (cmi->typ2>99) do_menu(&pmenu[cmi->typ2 - 100]);
  229.             refresh_menu(cm);
  230.             menu_hilight(cmi);
  231.         }
  232.  
  233.         for (si=NULL,mi=cm;mi->typ!=0;mi++) {
  234.             if (mi->y==cmi->y && mi->x > cmi->x)
  235.             if (si!=NULL) {
  236.                 if (mi->x < si->x)
  237.                     si = mi;
  238.             } else si = mi;
  239.         }
  240.  
  241.         break;
  242.       case edelete: /* delete */
  243.         emode = true;
  244.         if (cmi->val==NULL) break;
  245.         if (strlen(cmi->val)==0) break;
  246.         if (cx<1) break;
  247.         ncpy(mbuff,cmi->val,cx-1);
  248.         strcat(mbuff,cmi->val + cx);
  249.         setvstr(&cmi->val,mbuff);
  250.         cx--;
  251.         gotoxy(hcx+cx,hcy);
  252.         cputs(cmi->val + cx);
  253.         putch(DASHCHAR);
  254.         gotoxy(hcx+cx,hcy);
  255.         break;
  256.       case edrawit:
  257.         break;
  258.       case eshowerror:
  259.         break;
  260.       case esave: /* save file */
  261.         break;
  262.       default: /* normal key */
  263.         if (c<26  && c!=9) {fner("Key has no affect"); break;}
  264.         if (c>200)  fner("Unimplemented command");
  265.         else {
  266. /*     if (cmi->typ!=MTEXT  && cmi->typ!=MFILE) goto doreturn; */
  267.             emode = true;
  268.             if (cmi->val==NULL) setvstr(&cmi->val,"");
  269.             ncpy(mbuff,cmi->val,cx);
  270.             mbuff[cx] = c; mbuff[cx+1] = 0;
  271.             strcat(mbuff,cmi->val + cx);
  272.             setvstr(&cmi->val,mbuff);
  273.             gotoxy(hcx+cx,hcy);
  274.             cputs(cmi->val + cx);
  275.             cx++;
  276.             gotoxy(hcx+cx,hcy);
  277.         }
  278.         break;
  279.      }
  280.      if (si!=NULL) {
  281.         menu_norm(cmi);
  282.         cmi = si;
  283.         menu_hilight(cmi);
  284.         si = NULL;
  285.      }
  286.     }
  287. exit_menu:;
  288.     /* save current point */
  289.     { int i;
  290.         for (i=0,mi=cm;mi->typ!=0;mi++,i++) {
  291.           if (mi==cmi) ppmenu->ci = i;
  292.         }
  293.     }
  294.     return false; /* normal exit */
  295. }
  296. setvstr(char **d, char *s)
  297. {
  298.     if ((*d) != 0) myfree(*d);
  299.     *d = sdup(s);
  300. }
  301. int mystrlen(char *s)
  302. {
  303.     if (s==NULL) return 0;
  304.     else return strlen(s);
  305. }
  306. menu_hilight(struct menu_struct *mm)
  307. {
  308.     int i,j;
  309.     struct menu_struct *m=mm;
  310.     scr_menuhi();
  311.     hcx = m->x + mystrlen(m->title);
  312.     hcy = m->y;
  313.     gotoxy(hcx,hcy);
  314.     if (m->typ==MTOGON || m->typ==MTOGOFF) {
  315.         if (m->val==NULL) m->val = myallocz(4);
  316.         if (m->typ==MTOGON) strcpy(m->val,"ON ");
  317.         if (m->typ==MTOGOFF) strcpy(m->val,"OFF");
  318.     }
  319.     if (m->val==NULL) setvstr(&m->val,"");
  320.     cputs(m->val);
  321.     j = m->width - mystrlen(m->val);
  322.     printdash(j);
  323.     cx = mystrlen(m->val);
  324.     gotoxy(hcx+cx,hcy);
  325. }
  326. printdash(int j)
  327. {
  328.     int i;
  329.     for (i=0;i<j;i++) mbuff[i] = DASHCHAR;
  330.     mbuff[j] = 0;
  331.     if (j>0) cputs(mbuff);
  332. }
  333. menu_norm(struct menu_struct *mm)
  334. {
  335.     int i,j;
  336.     struct menu_struct *m=mm;
  337.     gotoxy(m->x + mystrlen(m->title),m->y);
  338.     if (m->typ==MTOGON  || m->typ==MTOGOFF) {
  339.         if (m->val==NULL) m->val = myallocz(4);
  340.         if (m->typ==MTOGON) strcpy(m->val,"ON ");
  341.         if (m->typ==MTOGOFF) strcpy(m->val,"OFF");
  342.     }
  343.     if (m->val != NULL) {
  344.         scr_menuval();
  345.         if (m->width>0) cputs(m->val);
  346.         scr_menubg();
  347.     }
  348.     j = m->width - mystrlen(m->val);
  349.     printdash(j);
  350. }
  351. refresh_menu(struct menu_struct *mm)
  352. {
  353.     int i;
  354.     struct menu_struct *m=mm;
  355.     window(1,1,80,24);
  356.     scr_menubg();
  357.     clrscr();
  358.     for (;m->typ!=0;m++) {
  359.         if (m->title != NULL) if (strlen(m->title)>0) {
  360.             gotoxy(m->x,m->y);
  361.             cputs(m->title);
  362.         }
  363.         menu_norm(m);
  364.     }
  365. }
  366. do_help(char *k1)
  367. {
  368.   char kbuff[80];
  369.   strcpy(kbuff,k1);
  370.     do_help2(kbuff,"");
  371. }
  372. do_help2(char *k1, char *k2)
  373. {
  374.     FILE *hfile;
  375.     int i,yy;
  376.     char hbuff[90];
  377.     char *hfilename="surface.hlp";
  378.     char fk1[20],fk2[20],*os,*si;
  379.     char savek1[40],subtopic[40];
  380.  
  381.  
  382. help_again:;
  383.     window(1,1,80,24);
  384.     strcpy(savek1,k1);
  385.     scr_menuval();
  386.     clrscr();
  387.     if (strcmp(k2," ")==0) strcpy(k2,"");
  388.     gotoxy(1,1); wprintf("SURFACE help on topic {%s},  sub topic {%s}",k1,k2);
  389.     scr_menubg();
  390.     hfile = fopen(gletopdir(hfilename),"rt");
  391.     if (hfile==NULL) perror("Unable to open surface.hlp ");
  392.     for (;!feof(hfile);) {
  393.         if (fgets(hbuff,90,hfile)!=NULL) {
  394.             if (hbuff[0]=='3') {
  395.                 strtok(hbuff," \n");
  396.                 strcpy(fk1,strtok(NULL," \n"));
  397.                 if (strcmp(fk1,k1)==0 && strlen(k2)==0)
  398.                     goto help_type;
  399.             }
  400.             if (hbuff[0]=='4') {
  401.                 strtok(hbuff," \n");
  402.                 strcpy(fk2,strtok(NULL," \n"));
  403.                 if (strlen(k2)>0)
  404.                 if (strcmp(fk1,k1)==0 &&
  405.                     strcmp(fk2,k2)==0) goto help_type;
  406.             }
  407.         }
  408.     }
  409.     fclose(hfile);
  410.     scr_menuval();
  411.     gotoxy(1,9);
  412.     wprintf("help text not found sorry ??? \n");
  413.     text_inkey();
  414.     return;
  415. help_type:;
  416.     yy = 3;
  417.     for (;!feof(hfile);) {
  418.         if (fgets(hbuff,90,hfile)!=NULL) {
  419.             if (isdigit(hbuff[0])) {
  420.                 goto end_help;
  421.             }
  422.             gotoxy(1,yy++); cputs(tabtospace(hbuff));
  423.         }
  424.         if (yy==22) {
  425.             scr_menuval();
  426.             gotoxy(2,24); wprintf("Press any key for next screen of help");
  427.             text_inkey();  clrscr();
  428.             gotoxy(1,1); wprintf("Graph help on topic {%s},  sub topic {%s}",k1,k2);
  429.             scr_menubg();  yy = 3;
  430.         }
  431.     }
  432. end_help:;
  433.     scr_menuval();
  434.     gotoxy(1,yy+1);
  435.     read_command(subtopic,"Subtopic [return to continue] ");
  436.     strupr(subtopic);
  437.     if (strlen(subtopic)!=0) {
  438.         fclose(hfile);
  439.            strcpy(k1,savek1);
  440.         k2 = subtopic;
  441.         goto help_again;
  442.     }
  443.     fclose(hfile);
  444. }
  445. add_unrecognized(char *s)
  446. {
  447.     if (nunrec>6) return;
  448. }
  449. int vunquote(char **s);
  450. vunquote(char **ss)
  451. {
  452.     char *s = *ss;
  453.     if (*s=='\"') {
  454.         s[strlen(s)-1] = 0;
  455.         *ss = sdup(s+1);
  456.         myfree(s);
  457.     }
  458. }
  459.  
  460. int initmenudone;
  461. menutype *m_pnt[20];
  462. menutype *m_end[20];
  463. char *m_val[20];
  464. char m_tog[50];
  465. initmenu()
  466. {
  467.     menutype *mi;
  468.     char *s,kw1[80],kw2[80];
  469.     int i,j,nt=0,k,z,ct,used;
  470.     static int nv;
  471.  
  472.     if (!initmenudone) {
  473.       initmenudone = true;
  474.       for (z=0;z<=10;z++) {
  475.         for (mi=pmenu[z].menu; mi->typ!=0; mi++) {
  476.         if (mi->typ==MTOGON || mi->typ==MTOGOFF) {
  477.             m_tog[nt++] = mi->typ;
  478.         }
  479.         if (mi->val!=NULL) {
  480.             m_val[nv] = sdup(mi->val);
  481.             mi->val = sdup(mi->val);
  482.             m_pnt[nv++] = mi;
  483.         }
  484.         }
  485.       }
  486.     } else {
  487.       for (z=0;z<=10;z++) {
  488.         for (mi=pmenu[z].menu; mi->typ!=0; mi++) {
  489.         if (mi->typ==MTOGON || mi->typ==MTOGOFF) {
  490.             mi->typ = m_tog[nt++];
  491.         } else {
  492.             if (mi->val!=0) {
  493.                 myfree(mi->val);
  494.             }
  495.             mi->val = 0;
  496.         }
  497.         }
  498.       }
  499.       for (z=0;z<nv;z++) {
  500.         mi = m_pnt[z];
  501.         mi->val = sdup(m_val[z]);
  502.       }
  503.     }
  504. }
  505.  
  506. char msend[200];
  507. #define mss msend+strlen(msend)
  508. char cs[80]="xyz";
  509. int gstartend;
  510. /* char msend[200]; */
  511. m_sendline()
  512. {
  513.     msend[0] = 0;
  514.     strcpy(cs,"xyz");
  515. }
  516. m_ifsend(char *s)
  517. {
  518.     if (strcmp(s,cs)!=0) {
  519.       m_sendline();
  520.       sprintf(msend,"\t%s ",s);
  521.       strcpy(cs,s);
  522.     }
  523. }
  524. int g_get_usersize(double *x, double *y);
  525. setmenu(menutype *m,int x, int y, int typ, int typ2, int width, char *title
  526. , char *val, char *help)
  527. {
  528.     m->x = x; m->y = y; m->typ = typ; m->typ2 = typ2; m->width = width;
  529.     m->title = sdup(title);
  530.     if (val!=0) m->val = sdup(val);
  531.     m->help = sdup(help);
  532. }
  533. init_menucolor()
  534. {
  535.         h_bcolor = WHITE; h_fcolor = BLACK;
  536. }
  537. int pick_file(char *result, char *wld)
  538. {
  539.     pmenutype ask;
  540.     char wild[80];
  541.     char buff2[80];
  542.     char *f[80];
  543.     char *swap;
  544.     menutype *m,*mi,*mwild;
  545.     struct ffblk ffblk;
  546.     int done,i,x,y,nf,rval,adddir=false;
  547.  
  548.     strcpy(wild,wld);
  549.     ask.ci = 0;
  550. pick_again:;
  551.     x=0; y=0; nf=0;
  552.     init_menucolor();
  553.     done = findfirst(wild,&ffblk,0);
  554.     for (nf=0;nf<70 && !done;nf++) {
  555.         f[nf] = sdup(ffblk.ff_name);
  556.         done = findnext(&ffblk);
  557.     }
  558. sortagain:;
  559.     swap = NULL;
  560.     for (i=0;i<(nf-1);i++) {
  561.         if (strcmp(f[i],f[i+1])>0) {
  562.             swap = f[i]; f[i] = f[i+1]; f[i+1] = swap;
  563.         }
  564.     }
  565.     if (swap != NULL) goto sortagain;
  566.  
  567.     m = myallocz((3+nf)*sizeof(*m));
  568.     ask.menu = m;
  569.     ask.ci = 0;
  570.     if (nf==0) ask.ci = 1;
  571.     if (m==0) wprintf("memory allocation error");
  572.     for (mi=m,i=0;i<nf;i++,mi++) {
  573.         setmenu(mi,x*20+1,y+5,MRETURN,0,18,"",f[i],"");
  574.         x++;
  575.         if (x==4) {x=0; y++; }
  576.     }
  577.     setmenu(mi,5,1,MNULL,0,0,"Use arrow keys to select file then press return","","");
  578.     mi++;
  579.     setmenu(mi,5,3,MRETURN,0,30,"Disk/Dir Specification ",wild,"");
  580.     mwild = mi;
  581.     mi++; mi->typ = 0;
  582.     rval = do_menu(&ask);
  583.     strcpy(result,((ask.menu)[ask.ci]).val);
  584.     for (i=0;i<nf;i++) myfree(f[i]);
  585.     myfree(m);
  586.  
  587.     if (strcmp(wild,mwild->val)!=0) {
  588.         strcpy(wild,mwild->val);
  589.         if (strstr(wild,"*")==NULL) {
  590.             if (wild[strlen(wild)-1] == '\\')
  591.                 strcat(wild,"*.gle");
  592.             else
  593.                 strcat(wild,"\\*.gle");
  594.         }
  595.         adddir = true;
  596.         goto pick_again;
  597.     }
  598.  
  599.     if (adddir) {
  600.         *(strfile(wild)) = 0;
  601. #ifdef __TURBOC__
  602.         if (strstr(wild,":")!=NULL) {
  603.             setdisk(toupper(*wild)-'A');
  604.         }
  605.         strcpy(buff2,wild+2); strcpy(wild,buff2);
  606. #endif
  607.         if (chdir(wild)!=0) {perror("Cannot change dir"); delay(3000);}
  608.     }
  609.  
  610. /*    if (adddir) {
  611.         *(strfile(wild)+1) = 0;
  612.         strcpy(result,wild);
  613.         strcat(result,((ask.menu)[ask.ci]).val);
  614.     }
  615. */
  616.     return rval;
  617. }
  618. char *strfile(char *s)
  619. {
  620.     char *e;
  621.     e = s;
  622.     for (;*s!=0;s++) {
  623.         if (*s==']'  ||  *s=='\\' || *s=='/' || *s==':') e = s;
  624.     }
  625.     return e;
  626. }
  627.